<?php
  class Lienhe extends Contact_Controller{
      public function __construct(){
          parent :: __construct();
          $this->load->model('Contact_m');
          $this->load->library('email');
      }
      
      function index(){
          $this->gui_lienhe();
      }
      
       function is_correct_captcha($captcha) {
        $expiration = time() - 1800;
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($captcha, $this->input->ip_address(), $expiration);
        $query = $this->db->query($sql, $binds);
        $row = $query->row();

        if ($row->count == 0) {
            $this->form_validation->set_message('is_correct_captcha', 'Please enter correct word in the picture!');
            return FALSE;
        }
        return TRUE;
        }
      
      function gui_lienhe(){
      /* Captcha */
       
//        $this->session->set_userdata('current_url', current_url());
//        $expiration_period = time() - 1800;
//        $this->db->query("DELETE FROM captcha WHERE captcha_time < " . $expiration_period);
//        $this->load->helper('captcha');
//        $vals = array(
//            'word' => strtoupper(random_string('alnum', 6)),
//            'img_path' => './assets/captcha/',
//            'img_url' => base_url() . 'assets/captcha/',
//            'font_path' => base_url() . 'system/fonts/texb.ttf',
//            'img_width' => '150',
//            'img_height' => 32,
//            'expiration' => 300
//        );
//
//        $cap = create_captcha($vals);
//
//        $cap_save = array(
//            'captcha_id' => '',
//            'captcha_time' => $cap['time'],
//            'ip_address' => $this->input->ip_address(),
//            'word' => $cap['word']
//        );
//        $query = $this->db->insert_string('captcha', $cap_save);
//        $this->db->query($query);
//        $this->_data['captcha_img'] = $cap['image'];
//        /* End of captcha */
//        
          $config = array(
                            array('field' => 'full_name',
                                  'label' => 'Họ tên',
                                  'rules' => 'required|trim|xss_clean'
                                 ),
                          
                                 
                            array('field' => 'email',
                                  'label' => 'Email',
                                  'rules' => 'required|trim|xss_clean|valid_email'
                                 ),
                            
                                 
                            array('field' => 'content',
                                  'label' => 'Nội dung',
                                  'rules' => 'required|trim|xss_clean'
                                 )
                           
          
                         );
          $this->form_validation->set_rules($config);
          
          if($this->input->post('send')){
              
              if($this->form_validation->run()){
                    
//                    $receiver = $this->Contact_m->get_receiver_email();
//                    $receiver_implode = implode(',' , $receiver);
//                    $receiver_array = explode(',', $receiver_implode);
//                   
//                    foreach($receiver_array as $emails){
//                        $destination = $emails['destination'];
//                        $from = $this->input->post('email');
//                        $subject = "Contact Email";
//                        $message = $this->input->post('content');
//                        $fullname = $this->input->post('first_name')." ".$this->input->post('last_name');
//                    
//                        $this->email->from($from, $fullname);     
//                        $this->email->to($destination);
//                        
//                        $this->email->subject($subject);
//                        $this->email->message($message);
//
//                        $this->email->send();    
//                    }
                                       
                    
                  
                    $full_name = $this->input->post('full_name');
                    $email = $this->input->post('email');
                    $content = $this->input->post('content');
                    $posttime = now();
                    
                    $data = array(
                                    'full_name' => $full_name,
                                    'email'      => $email,
                                    'content'    => $content,
                                    'posted_time' => $posttime
                                 );
                    $query = $this->Contact_m->add_contact($data);
                    
                    if($query > 0){
                        $this->session->set_flashdata('error', "Gửi yêu cầu thành công");    
                    }else{
                        $this->session->set_flashdata('error', "Gửi yêu cầu thất bại");    
                    }
                    redirect('lienhe/gui_lienhe');    
                  
              }
              
          }
          
          $this->display_view('home/contact/send_contact.php');  
          
          if($this->input->post('reset')){
               redirect('lienhe/gui_lienhe');  
          }           
              
          
      }
  }
?>
